package com.kalix.ksh.record.biz;

import com.kalix.framework.core.api.persistence.JsonData;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.model.InfUtil;
import com.kalix.ksh.proint.api.model.TimeUtil;
import com.kalix.ksh.proint.entities.LineChartRequestBean;
import com.kalix.ksh.record.api.biz.ILineRecordBeanService;
import com.kalix.ksh.record.api.dao.ILineRecordBeanDao;
import com.kalix.ksh.record.entities.LineRecordBean;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Wanghw on 2018/10/12.
 * 折线图数据
 */
public class LineRecordServiceImpl extends GenericBizServiceImpl<ILineRecordBeanDao, LineRecordBean> implements ILineRecordBeanService {
    public LineRecordServiceImpl(){
        super.init(LineRecordBean.class.getName());
    }
/*
    public RecordData getAllLinerecord(Integer page, Integer limit, String jsonStr, String sort) {

        JsonData jd = getAllEntityByQuery(1, 10000, "", "");
        List<LineRecordBean> datas = jd.getData();
        Map<String, List<LineRecordBean>> maps = new LinkedHashMap<>();

        List<String> names = new ArrayList<>();
        for (LineRecordBean cr : datas) {
            if (!names.contains(cr.getName())){
                names.add(cr.getName());
            }
        }

        RecordData rd = new RecordData();
        List<RecordMdl> rds =new ArrayList<>();
        for (String name : names){
            List<LineRecordBean> nDatas = datas.stream().filter((n)->name.equals(n.getName())).collect(Collectors.toList());
            RecordMdl rm = new RecordMdl();
            rm.setName(name);
            rm.setData(nDatas);
            rds.add(rm);
        }
        rd.setData(rds);

        return rd;

//        JsonData jd = getAllEntityByQuery(page, limit, jsonStr);
//        RecordData rd = new RecordData();
//        RecordMdl rm = new RecordMdl();
//        rm.setName("线路一");
//        List datas = jd.getData();
//        rm.setData(datas);
//        List<RecordMdl> rds =new ArrayList<>();
//        rds.add(rm);
//        rd.setData(rds);
//        return rd;
    }*/
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");

public String getLineRequest() {
    String sql = "select uuid,name from LineChartRequestBean ";
    List<LineChartRequestBean> list = dao.find(sql);
    // System.out.println(list);
    String sTime = TimeUtil.getCurrentTime();
    String lTime = TimeUtil.getPastTime();
    String str = "";
    for(LineChartRequestBean bean:list) {
        String uuid = bean.getUuid();
        String name = bean.getName();
        String url = "http://"+wsIp+":"+wsPort+"/metrics/Metrics.moPath=\"Line.domain=\"defaultEngine\",uuid=\""+uuid+"\"\",name=\""+name+"\"?fresh=1860";
        double number = getData(url);
        str+=number +"," ;
    }
    // System.out.println(str);
    return str;
}


    public  double getData(String url){
       // System.out.println(url);
        String  data= InfUtil.interfaceUtil(url,"");
        // System.out.println(data);
        JSONObject obj = new JSONObject(data);
        //System.out.println(obj);
        double min= obj.getJSONObject("value").getDouble("value");
        //System.out.println(min);
        return min;
    }

}
